/*
 *Copyright (c) 2024 Black Sesame Technologies
 *
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License.
 *You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *Unless required by applicable law or agreed to in writing, software
 *distributed under the License is distributed on an "AS IS" BASIS,
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *See the License for the specific language governing permissions and
 *limitations under the License.
*/

#ifndef _IPC_TRANS_SES_MGT_H
#define _IPC_TRANS_SES_MGT_H

#include "bst_ipc_config.h"
#include "ipc_serdes.h"
#include "ipc_trans_common.h"
#include "ipc_trans_msg_mgt.h"
#ifdef __cplusplus
extern "C" {
#endif
#define DEFAULT_SES_ID 0
#define SES_UPDATE_STS_BIT 1
#define SES_UPDATE_MSG_BIT 2
#define SES_INFO_INVALID_VAL 16

#ifdef ENABLE_REMOTE_LOG_PROCESS_FUNC
#define REMOTE_LOG_SES_SID (SESSION_COUNT - 1)
#define REMOTE_LOG_SES_FID (CHANNEL_COUNT - 1)
#define REMOTE_LOG_SES_ID (((REMOTE_LOG_SES_SID) << 4) | (REMOTE_LOG_SES_FID))
#endif

struct _ses_base_t {
	uint8_t sid;
	uint8_t fid;
	uint8_t role;
	uint8_t cid;
};
#define ses_base_t struct _ses_base_t

enum _ses_status_t {
	SES_STATE_INVALID = 0,
	SES_STATE_NOT_AVAIL,
	SES_STATE_DESTROY,
	SES_STATE_AVAIL,
};
#define ses_status_t enum _ses_status_t

struct _client_msg_buf_t {
	bst_msg_queue_t reply_buf;
	bst_msg_queue_t signal_buf;
};
#define client_msg_buf_t struct _client_msg_buf_t

struct _server_msg_buf_t {
	bst_msg_queue_t method_buf;
};
#define server_msg_buf_t struct _server_msg_buf_t

struct _ipc_ses_t {
	ses_base_t info;
	ses_status_t status;
	ipc_ses_role_t ses_type;
	uint8_t update_flag;
	uint8_t cid_idx;
	union {
		client_msg_buf_t client_buf;
		server_msg_buf_t server_buf;
	};
};
#define ipc_ses_t struct _ipc_ses_t

static inline int8_t session_comb(uint8_t sid, uint8_t fid, uint8_t *info)
{
	*info = ((sid << 4) | fid);
	return 0;
}

int8_t session_mgt_init(void *addr);
int8_t session_register(const ses_base_t info, uint8_t *session_id, void *addr);
int8_t session_destroy(const uint8_t session_id, void *addr);
int8_t session_msg_in(const uint8_t session_id, const rw_msg_t *msg,
		      void *addr);
int8_t session_msg_out(const uint8_t session_id, const uint8_t type,
		       serdes_t *msg, void *addr);
int8_t session_rwmsg_out(const uint8_t session_id, rw_msg_t *msg, void *addr);

#ifdef __cplusplus
}
#endif
#endif
